package com.cysion.study.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cysion.study.R;
import com.cysion.study.entity.FamilyeduCelcrsEntity;
import com.cysion.study.ktbox.utils.CLog;
import com.cysion.study.ui.login.WebViewActivity;


/**
 * Created by chu on 2020-02-28.
 */

public class TvChannelItem extends LinearLayout {

    private TextView tv_time;
    private TextView tv_num;
    private TextView tv_status;
    private TextView tv_week;
    private TextView tv_cancel;
    private LinearLayout ll_btn;

    private float downX;
    private float downY;
    private int touchSlop;
    private boolean touchMode;
    private boolean slide;
    private int lastScrollX;

    public TvChannelItem(Context context) {
        super(context, null);
    }

    public TvChannelItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TvChannelItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setOrientation(HORIZONTAL);

        LayoutInflater.from(context).inflate(R.layout.widget_tv_channel, this);
        tv_time = findViewById(R.id.tv_time);
        tv_num = findViewById(R.id.tv_num);
        tv_status = findViewById(R.id.tv_status);
        tv_week = findViewById(R.id.tv_week);
        tv_cancel = findViewById(R.id.tv_cancel);
        ll_btn = findViewById(R.id.ll_btn);

        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //记录按下的位置
                downX = event.getRawX();
                downY = event.getRawY();
                lastScrollX = getScrollX();
                break;
            case MotionEvent.ACTION_MOVE:
                float nowX = event.getRawX();
                float nowY = event.getRawY();

                //判断用户是上下滑动还是左右滑动
                if (!touchMode && (Math.abs(nowX - downX) > touchSlop || Math.abs(nowY - downY) > touchSlop)) {
                    touchMode = true;   //一旦该变量被置为true，则滑动方向确定
                    if (Math.abs(nowX - downX) > touchSlop && Math.abs(nowY - downY) <= touchSlop) {
                        slide = true;   //此时认为是左右滑动
                        getParent().requestDisallowInterceptTouchEvent(true);   //请求父控件不要拦截触摸事件

                        //一下代码避免出发点击事件
                        MotionEvent cancelEvent = MotionEvent.obtain(event);
                        cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                        onTouchEvent(cancelEvent);
                    }
                }
                //左右滑动
                if (slide) {
                    float diffX = downX - nowX + lastScrollX;
                    if (diffX < 0)
                        diffX /= 3;
                    else if (diffX > ll_btn.getWidth())
                        diffX = (diffX - ll_btn.getWidth()) / 3 + ll_btn.getWidth();

                    scrollTo((int) diffX, 0);   //滑动处理
                }

                break;
            case MotionEvent.ACTION_UP:
                if (slide) {    //如果是左右滑动，那么松手时需要自动滑到指定位置
                    ValueAnimator animator;     //使用的是ValueAnimator，而非Scroller
                    if (getScrollX() > ll_btn.getWidth() / 2) {
//                        animator = ValueAnimator.ofInt(getScrollX(), ll_btn.getWidth());
                        animator = ValueAnimator.ofInt(getScrollX(), 140);
                    } else {
                        animator = ValueAnimator.ofInt(getScrollX(), 0);
                    }
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            scrollTo((Integer) animation.getAnimatedValue(), 0);
                        }
                    });
                    animator.start();
                    slide = false;
                }
                touchMode = false;  //重置变量
                break;
        }

        return super.onTouchEvent(event);
    }

    public void setChannel(final FamilyeduCelcrsEntity obj) {
        scrollTo(0, 0);

        tv_time.setText(obj.getStartTimeStr());
        tv_num.setText(obj.getCrsNum());
        //status":1//状态：1、待确认；2、家长拒绝；3、家长同意；4、系统自动同意；5、教师撤销
        if (obj.getStatus().equals("1")) {
            tv_status.setText("待确认");
        } else  if (obj.getStatus().equals("2")) {
            tv_status.setText("家长拒绝");
        } else  if (obj.getStatus().equals("3")) {
            tv_status.setText("家长同意");
        } else  if (obj.getStatus().equals("4")) {
            tv_status.setText("系统自动同意");
        } else  if (obj.getStatus().equals("5")) {
            tv_status.setText("教师撤销");
        }
        //提醒
        tv_week.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                ToastUtilKt.ShowToast("提醒");
                ValueAnimator animator;
                animator = ValueAnimator.ofInt(getScrollX(), 0);

                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
//                        scrollTo((Integer) animation.getAnimatedValue(), 0);
                        animator.clone();
                        scrollTo(0, 0);
                        if (onIemClick != null) {
                            onIemClick.onItemDetailClick(1);
                        }
                    }
                });
                animator.start();
//                scrollTo(0, 0);
            }
        });
        //撤销
        tv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                ToastUtilKt.ShowToast("撤销");
//                scrollTo(0, 0);
                ValueAnimator animator;
                animator = ValueAnimator.ofInt(getScrollX(), 0);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
//                        scrollTo((Integer) animation.getAnimatedValue(), 0);
                        scrollTo(0, 0);
                        if (onIemClick != null) {
                            onIemClick.onItemDetailClick(2);
                        }
                    }
                });
                animator.start();
            }
        });

        /*setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtilKt.ShowToast("条目");
            }
        });*/
    }

    private OnIemClick  onIemClick;

    public  interface OnIemClick {
        void onItemDetailClick(int type);
    }

    public void setOnItemClickListener(OnIemClick onIemClick) {
        this.onIemClick = onIemClick;
    }


}